/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.model.functions.environment;


import com.nebrija.riif.model.Environment;
import com.nebrija.riif.model.Function;
import com.nebrija.riif.model.RIIFProperties;
import com.nebrija.riif.model.RootClass;


/**
 * Function for environment information
 * @author asanchep
 *
 */
public class GetValue extends Function{

		//TODO: We ignore the 10^9 value as we suppose that the resulting value is in the same units as the operands
	
		public static final String NAME="environment.getValue";
		
	/**
		 * Creates an object
		 */
		public GetValue() {
			super(GetValue.NAME, -1);
		}

		@Override
		protected Object executeFunction(Object... operands) {
			Environment env = RootClass.getInstance().getActiveEnvironment();
			// there must be at least one operand with the number of failures to compare to
			if(env==null){return null;}
			RIIFProperties prop = env.getPropertiesElement(operands[0].toString());
			Object res = prop.getProperty(RIIFProperties.VALUE_KEY);
			if(res==null){
				res=prop.getProperty(RIIFProperties.RATE_KEY);
			}
			return res;
		}
		
		/**
		 * Returns the original string
		 * @param operands the original operands
		 * @return the string
		 */
		protected Object keepString(Object[] operands){
			StringBuilder notResolved = new StringBuilder();
			for (int i=1; i<operands.length; i++){
				notResolved.append(",").append(operands[i]);
			}
			return NAME+"("+notResolved.toString()+")";
		}
		
}